<?php

$result = isset($_SESSION['phone']) ? $_SESSION['phone'] : null;
$time = isset($_SESSION['time']) ? $_SESSION['time'] : null;

// 如果当前会话没有phone
if (empty($result) || empty($time) || time() > $time + 30 * 60) {
    // 先锁定session
    $_SESSION['phone'] = array();

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://nuoche.51hutui.com/mvcar/api/call/defaultnumber',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS => 'phone=8C89E86DAF199D4804374F605094C4CD&openId=&qrcodeId=1620534881900NxkuV&channel=11&isH5=true',
        CURLOPT_HTTPHEADER => array(
            'Host: nuoche.51hutui.com',
            'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0',
            'Content-Type: application/json',
            'app-auth-token:',
            'X-Requested-With: XMLHttpRequest',
            'Content-Length: 95',
            'Origin: https://nuoche.51hutui.com',
            'Connection: keep-alive',
            'Referer: https://nuoche.51hutui.com/mvcar/client/call.html?randomtime=1622170100282&phone=8C89E86DAF199D4804374F605094C4CD&qrcodeId=1620534881900NxkuV&channel=11&isH5=true&area=3202'
        ),
    ));

    $response = curl_exec($curl);

    curl_close($curl);

    $result = json_decode($response);

    // 为session 重新赋值
    $_SESSION['phone'] = $result;
    $time = time();
    $_SESSION['time'] = $time;
}

?>

<!doctype html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>临时号码</title>

    <style>
        h1 {
            text-align: center;
            font-size: 32px;
            margin: 25px;
        }

        .title {
            margin: 15px;
            text-align: center;
            font-size: 20px;
            font-weight: bold;
        }

        .phone {
            color: orange;
            font-size: 28px;
            font-weight: bold;
            display: block;
            text-align: center;
            margin: 15px auto;
        }
    </style>
</head>
<body>
<h1>
    Hi 你好~
</h1>
<div>
    <?php
    if ($result->flag != '200') {
        echo '请求过于频繁，请过几分钟后再试！';
        echo $result->message;
    } else {
        echo '<p class="title">请使用下面的电话联系我，有效时间剩余' . (round(30 - (time() - $time) / 60, 0)) . '分钟。</p>';
        echo '<a class="phone" href="tel:' . $result->datas . '">' . $result->datas . '</a>';
    }
    ?>
</div>
</body>
</html>

